from urllib.request import urlopen
from urllib.request import Request
from random import choice

url = "http://www.baidu.com"

# 三种浏览器的User-Agent：谷歌、IE、欧朋、火狐、搜狗、QQ浏览器，将让其随机产生取爬去服务器信息
user_agents=["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36",
             "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko",
             "Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11",
             "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"
             "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)",
             "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)",
             "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)"
             ]

# User-Agent就是伪装成浏览器，告诉服务器我是从浏览器访问的
headers = {
    "User-agent": choice(user_agents)
}
request = Request(url, headers=headers)
# print(request.get_header('User-agent'))
response = urlopen(request)

info = response.read()

# decode里面不填参数默认是utf-8
print(info.decode())
